package org.jeecg.modules.compilation.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.compilation.dto.CiDocTypeDTO;
import org.jeecg.modules.compilation.dto.ScreenDTO;
import org.jeecg.modules.compilation.dto.SearchcIDocDraftDTO;
import org.jeecg.modules.compilation.dto.TypeDTO;
import org.jeecg.modules.compilation.entity.CiDoc;
import org.jeecg.modules.compilation.entity.CiDocType;
import org.jeecg.modules.compilation.entity.CiSysUser;
import org.jeecg.modules.compilation.service.ICiDocTypeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.compilation.service.ICiSysUserService;
import org.jeecg.modules.compilation.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ci_doc_type
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Api(tags="ci_doc_type")
@RestController
@RequestMapping("/compilation/ciDocType")
@Slf4j
public class CiDocTypeController extends JeecgController<CiDocType, ICiDocTypeService> {
	@Autowired
	private ICiDocTypeService ciDocTypeService;
	@Autowired
	private ICiSysUserService ciSysUserService;

	/**
	 * 下拉框
	 * @return
	 */
	@AutoLog(value = "下拉框")
	@ApiOperation(value="下拉框", notes="下拉框")
	@GetMapping("/queryList")
	public Result<List<CiDocTypeDTO>> queryList(){
		return Result.OK( ciDocTypeService.queryList());
	}
	/**
	 * 分页列表查询
	 *
	 * @param ciDocType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "ci_doc_type-分页列表查询")
	@ApiOperation(value="ci_doc_type-分页列表查询", notes="ci_doc_type-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CiDocType>> queryPageList(CiDocType ciDocType,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CiDocType> queryWrapper = QueryGenerator.initQueryWrapper(ciDocType, req.getParameterMap());
		Page<CiDocType> page = new Page<CiDocType>(pageNo, pageSize);
		IPage<CiDocType> pageList = ciDocTypeService.page(page, queryWrapper);
		List<CiSysUser> ciSysUserList = ciSysUserService.list();
		for (CiDocType record : pageList.getRecords()) {
			for (CiSysUser ciSysUser : ciSysUserList) {
				if(record.getUpdateBy().equals(ciSysUser.getId())){
					record.setUpdateBy(ciSysUser.getRealname());
					break;
				}
			}
		}
		return Result.OK(pageList);
	}
	 /*
	  * 显示title标题
	  * */
	 @ApiOperation(value="ci_doc_type-筛选", notes="ci_doc_type-筛选")

	 @RequestMapping(value = "/title", method = RequestMethod.POST)
	 public Result<TypeDTO> title() {
		 return Result.OK(ciDocTypeService.title());
	 }
	 /*
	  * 根据公文类型和时间，部门名字查询
	  *
	  * */
	 @ApiOperation(value="ci_doc_type-类型查询", notes="ci_doc_type-类型查询")

	 @RequestMapping(value = "/search", method = RequestMethod.POST)
	 public Result<List<SearchcIDocDraftDTO>> search(@RequestBody ScreenDTO titleDTO) throws IOException {
		 return Result.OK(ciDocTypeService.search(titleDTO));
	 }
	
	/**
	 *   添加
	 *
	 * @param ciDocType
	 * @return
	 */
	@AutoLog(value = "ci_doc_type-添加")
	@ApiOperation(value="ci_doc_type-添加", notes="ci_doc_type-添加")
//	@RequiresPermissions("compilation:ci_doc_type:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CiDocType ciDocType) {
		ciDocTypeService.save(ciDocType);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ciDocType
	 * @return
	 */
	@AutoLog(value = "ci_doc_type-编辑")
	@ApiOperation(value="ci_doc_type-编辑", notes="ci_doc_type-编辑")
//	@RequiresPermissions("compilation:ci_doc_type:edit")
	@RequestMapping(value = "/updateOrEdit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CiDocType ciDocType) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = loginUser.getId();
		ciDocType.setUpdateBy(userId);
		ciDocType.setUpdateTime(new Date());
		if(StringUtils.isEmpty(ciDocType.getDocumentTypeSn())){
			ciDocType.setDocumentTypeSn(CommonUtil.getSn());
		}
		ciDocTypeService.saveOrUpdate(ciDocType);
		return Result.OK("成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ci_doc_type-通过id删除")
	@ApiOperation(value="ci_doc_type-通过id删除", notes="ci_doc_type-通过id删除")
//	@RequiresPermissions("compilation:ci_doc_type:delete")
	@GetMapping(value = "/removeBySn")
	public Result<String> delete(@RequestParam(name="documentTypeSn",required=true) String documentTypeSn) {
		LambdaQueryWrapper<CiDocType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(CiDocType::getDocumentTypeSn,documentTypeSn);
		ciDocTypeService.remove(lambdaQueryWrapper);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ci_doc_type-批量删除")
	@ApiOperation(value="ci_doc_type-批量删除", notes="ci_doc_type-批量删除")
//	@RequiresPermissions("compilation:ci_doc_type:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ciDocTypeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "ci_doc_type-通过id查询")
	@ApiOperation(value="ci_doc_type-通过id查询", notes="ci_doc_type-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CiDocType> queryById(@RequestParam(name="id",required=true) String id) {
		CiDocType ciDocType = ciDocTypeService.getById(id);
		if(ciDocType==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ciDocType);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ciDocType
    */
//    @RequiresPermissions("compilation:ci_doc_type:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CiDocType ciDocType) {
        return super.exportXls(request, ciDocType, CiDocType.class, "ci_doc_type");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("compilation:ci_doc_type:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CiDocType.class);
    }

}
